package com.itheima.service.mongo.impl;

import cn.hutool.core.collection.CollUtil;
import com.itheima.domain.mongo.UserLike;
import com.itheima.domain.vo.PageBeanVo;
import com.itheima.service.mongo.UserLikeCountService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@DubboService
public class UserLikeCountServiceImpl implements UserLikeCountService {
    @Autowired
    private MongoTemplate mongoTemplate;
    @Override
    public void addUserLike(UserLike userLike) {
        mongoTemplate.save(userLike);
    }
    @Override
    public boolean common(Long userId, Long cardsId) {
        Query query1 = new Query(
                Criteria.where("userId").is(userId)
                        .and("likeUserId").is(cardsId)
        );

        Query query2 = new Query(
                Criteria.where("userId").is(cardsId)
                        .and("likeUserId").is(userId)
        );

        boolean exists1 = mongoTemplate.exists(query1, UserLike.class);
        boolean exists2 = mongoTemplate.exists(query2, UserLike.class);

        if(exists1 && exists2){
            return true;
        }
        return false;
    }

    //相互喜欢的数量
    @Override
    public int eachLikeCount(Long userId) {
        //首先查询我的喜欢列表
        Query query = new Query(
                Criteria.where("userId").is(userId)
        );
        List<UserLike> userLikes = mongoTemplate.find(query, UserLike.class);


        //将我的喜欢列表中的用户id添加进去
        List<Long> likeUserid = new ArrayList<>();
        for (UserLike userLike : userLikes) {
            likeUserid.add(userLike.getLikeUserId());
        }

        //在我的喜欢列表范围内查找喜欢我的人有哪些
        Query query1 = new Query(
                Criteria.where("userId").in(likeUserid).and("likeUserId").is(userId)
        );

        List<UserLike> eachLikeList = mongoTemplate.find(query1, UserLike.class);

        return eachLikeList.size();
    }

    @Override
    public int loveCount(Long userId) {
        List<UserLike> loveList = mongoTemplate.find(Query.query(Criteria.where("userId").is(userId)),UserLike.class);

        return loveList.size();
    }

    @Override
    public int fanCount(Long userId) {
        List<UserLike> fanList =mongoTemplate.find(Query.query(Criteria.where("likeUserId").is(userId)),UserLike.class);
        return fanList.size();
    }

    @Override
    public PageBeanVo eachLikeList(Long userId, Integer pageNum, Integer pageSize) {
        //计算分页页码
        int startIndex = (pageNum-1)*pageSize;
        //首先查询我的喜欢列表
        Query query = new Query(
                Criteria.where("userId").is(userId)
        ).with(Sort.by(Sort.Order.desc("created")))//根据喜欢时间排序
                .skip(startIndex).limit(pageSize);//分页

        List<UserLike> userLikes = mongoTemplate.find(query, UserLike.class);

        //将我的喜欢列表中的用户id添加进去
        List<Long> likeUserId = new ArrayList<>();
        for (UserLike userLike : userLikes) {
            likeUserId.add(userLike.getLikeUserId());
        }

        //在我的喜欢列表范围内查找喜欢我的人有哪些
        Query query1 = new Query(
                Criteria.where("userId").in(likeUserId).and("likeUserId").is(userId)
        );

        List<UserLike> eachLikeList = mongoTemplate.find(query1, UserLike.class);
        long count = mongoTemplate.count(query1, UserLike.class);

        return new PageBeanVo(pageNum,pageSize,count,eachLikeList);
    }

    @Override
    public PageBeanVo loveCountList(Long userId, Integer pageNum, Integer pageSize) {
        //计算分页页码
        int startIndex = (pageNum-1)*pageSize;
        //首先查询我的喜欢列表
        Query query = new Query(
                Criteria.where("userId").is(userId)
        ).with(Sort.by(Sort.Order.desc("created")))//根据喜欢时间排序
                .skip(startIndex).limit(pageSize);//分页

        List<UserLike> loveList = mongoTemplate.find(query, UserLike.class);
        long count = mongoTemplate.count(query, UserLike.class);
        return new PageBeanVo(pageNum,pageSize,count,loveList);
    }

    @Override
    public PageBeanVo fanList(Long userId, Integer pageNum, Integer pageSize) {
        //计算分页页码
        int startIndex = (pageNum-1)*pageSize;
        //首先查询喜欢我的列表
        Query query = new Query(
                Criteria.where("likeUserId").is(userId)
        );
        List<UserLike> fanList = mongoTemplate.find(query, UserLike.class);

        long count = mongoTemplate.count(query, UserLike.class);

        return new PageBeanVo(pageNum,pageSize,count,fanList);
    }

    @Override
    public void deleteCards(Long userId, Long cardsId) {
        Query query = new Query(
                Criteria.where("userId").is(userId)
                .and("likeUserId").is(cardsId)
        );
        mongoTemplate.remove(query,UserLike.class);
    }


}
